package com.example.smartmedicalsystem;

import com.example.smartmedicalsystem.entity.Department;
import com.example.smartmedicalsystem.entity.Medicine;
import com.example.smartmedicalsystem.entity.User;
import com.example.smartmedicalsystem.repository.DepartmentRepository;
import com.example.smartmedicalsystem.repository.MedicineRepository;
import com.example.smartmedicalsystem.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.List;

@Configuration
@RequiredArgsConstructor
public class DataInitializer {
    private final UserRepository userRepository;
    private final DepartmentRepository departmentRepository;
    private final MedicineRepository medicineRepository;
    private final PasswordEncoder passwordEncoder;

    @Bean
    public CommandLineRunner loadAdmin() {
        return args -> {
            if (userRepository.findByUsername("admin") == null) {
                User admin = new User();
                admin.setUsername("admin");
                admin.setPassword(passwordEncoder.encode("admin123"));
                admin.setRole(User.Role.ADMIN);
                userRepository.save(admin);
            }
        };
    }

    @Bean
    public CommandLineRunner loadDepartments() {
        return args -> {
            if (departmentRepository.count() == 0) {
                List<Department> departments = List.of(
                        new Department(null, "内科", "处理内科疾病"),
                        new Department(null, "外科", "处理外科手术"),
                        new Department(null, "儿科", "处理儿童疾病"),
                        new Department(null, "心血管科", "心脏与血管疾病治疗"),
                        new Department(null, "神经科", "神经系统疾病诊疗"),
                        new Department(null, "骨科", "骨骼与关节疾病治疗"),
                        new Department(null, "皮肤科", "皮肤相关疾病治疗"),
                        new Department(null, "眼科", "眼部疾病诊疗"),
                        new Department(null, "耳鼻喉科", "耳鼻喉相关疾病治疗"),
                        new Department(null, "急诊科", "紧急医疗救治")
                );
                departmentRepository.saveAll(departments);
            }
        };
    }

    @Bean
    public CommandLineRunner loadMedicines() {
        return args -> {
            if (medicineRepository.count() == 0) {
                List<Medicine> medicines = List.of(
                        new Medicine(null, "阿莫西林", "广谱抗生素", 1000),
                        new Medicine(null, "布洛芬", "解热镇痛", 1000),
                        new Medicine(null, "头孢克肟", "第三代头孢菌素", 1000),
                        new Medicine(null, "对乙酰氨基酚", "退热止痛", 1000),
                        new Medicine(null, "奥美拉唑", "胃酸抑制剂", 1000),
                        new Medicine(null, "氯雷他定", "抗过敏药物", 1000),
                        new Medicine(null, "二甲双胍", "糖尿病治疗药物", 1000),
                        new Medicine(null, "阿托伐他汀", "降血脂药物", 1000),
                        new Medicine(null, "氨氯地平", "降压药物", 1000),
                        new Medicine(null, "胰岛素注射液", "糖尿病治疗", 1000),
                        new Medicine(null, "阿司匹林", "抗血小板聚集", 1000),
                        new Medicine(null, "硝酸甘油", "心绞痛急救", 1000),
                        new Medicine(null, "孟鲁司特钠", "哮喘治疗", 1000),
                        new Medicine(null, "左氧氟沙星", "广谱抗菌药", 1000),
                        new Medicine(null, "维生素C片", "补充维生素C", 1000),
                        new Medicine(null, "葡萄糖酸钙", "补钙制剂", 1000),
                        new Medicine(null, "地西泮", "镇静安眠药", 1000),
                        new Medicine(null, "肾上腺素注射液", "急救用药", 1000),
                        new Medicine(null, "氢氯噻嗪", "利尿降压", 1000),
                        new Medicine(null, "复合维生素B", "营养补充剂", 1000)
                );
                medicineRepository.saveAll(medicines);
            }
        };
    }
}